﻿using MyApp.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyApp.DataModels;
using SqlSugar;
using MyApp.IRepository;
using FullCoreDBModels;
using Oracle.ManagedDataAccess.Client;
using Newtonsoft.Json;
using System.IO;
using MyApp.DataModels.DTO;
using MyApp.Common;

namespace MyApp.Repository
{
    public class InterfaceToolRepository : BaseRepository<InterfaceToolEntity>, IInterfaceToolRepository
    {
        #region 变量

        private readonly string deleteInterfaceToolSql;
        private readonly string insertInterfaceToolSql;
        private readonly string updateInterfaceToolSql;
        #endregion
        public InterfaceToolRepository(SqlSugarScope sqlSugarScope) : base(sqlSugarScope)
        {
            var jsonFile = Path.Combine( AppDomain.CurrentDomain.BaseDirectory , "JsonSql","InterfaceTool.json");
            LogHelper.GetLog().Info("jsonsql地址"+jsonFile);
            var orderJsonSqls = JsonConvert.DeserializeObject<List<JsonSql>>(File.ReadAllText(jsonFile));
            if (orderJsonSqls.Any())
            {
                deleteInterfaceToolSql = orderJsonSqls.FirstOrDefault(a => a.SqlKey.Equals("deleteInterfaceToolSql"))?.SqlValue;
                insertInterfaceToolSql = orderJsonSqls.FirstOrDefault(a => a.SqlKey.Equals("insertInterfaceToolSql"))?.SqlValue;
                updateInterfaceToolSql = orderJsonSqls.FirstOrDefault(a => a.SqlKey.Equals("updateInterfaceToolSql"))?.SqlValue;
            }
            else
            {
                throw new Exception("InterfaceTool.json语句文件加载出错！");
            }
        }
        public string Test()
        {
            return db.Ado.ExecuteCommand("insert into patient (Id,Name) values('222','张三')").ToString();
        }

        #region 方法

        public List<InterfaceToolEntity> GetInterfaceTool(List<ConditionArgs> conditions)
        {
            if (conditions != null)
            {
                var where = ConditionArgsHelper.JoinSQLStr(conditions.ToArray());
                string sql = "SELECT * FROM INTERFACE_TOOL where 1=1 ";
                if (!string.IsNullOrEmpty(where))
                {
                    sql = sql + where;
                }

                var result = db.Ado.SqlQuery<InterfaceToolEntity>(sql).ToList();
                return result;

            }
            else
            {
                return null;
            }
        }

        public CommonResult SaveInterfaceTool(List<InterfaceToolEntity> interfaces)
        {
            var result = new CommonResult();

            try
            {

                db.Ado.BeginTran();
                try
                {
                    interfaces.ForEach(a =>
                    {

                        if (a.ISDELETE == 1)
                        {
                            db.Ado.ExecuteCommand(deleteInterfaceToolSql, new { a.ID });
                        }
                        else if (!string.IsNullOrEmpty(a.ID))
                        {
                            SugarParameter[] param = new SugarParameter[]{
                                    new SugarParameter(":INTERFACECODE",a.INTERFACECODE),
                                    new SugarParameter(":INTERFACENAME" , a.INTERFACENAME),
                                    new SugarParameter(":INTERFACEDESCRIPTION", a.INTERFACEDESCRIPTION),
                                    new SugarParameter(":INJSON", a.INJSON),
                                    new SugarParameter(":JAVASCRIPTCODE", a.JAVASCRIPTCODE),
                                    new SugarParameter(":OUTJSON", a.OUTJSON),
                                    new SugarParameter(":CLASSNAME", a.CLASSNAME),
                                    new SugarParameter(":FUNCTIONNAME", a.FUNCTIONNAME),
                                    new SugarParameter(":ID", a.ID),
                                    new SugarParameter(":CREATEDATE", a.CREATEDATE),
                                    new SugarParameter(":CREATEUSER", a.CREATEUSER),
                                    new SugarParameter(":UPDATEDATE", a.UPDATEDATE),
                                    new SugarParameter(":UPDATEUSER", a.UPDATEUSER),
                                    new SugarParameter(":SCRIPTTYPE", a.SCRIPTTYPE),
                                    new SugarParameter(":INVALIDFLAG", a.INVALIDFLAG),
                            };
                            db.Ado.ExecuteCommand(updateInterfaceToolSql, param);
                        }
                        else if (string.IsNullOrEmpty(a.ID))
                        {
                            a.ID = Guid.NewGuid().ToString();
                            a.INVALIDFLAG = 0;
                            SugarParameter[] param = new SugarParameter[]{
                                    new SugarParameter(":INTERFACECODE",a.INTERFACECODE),
                                    new SugarParameter(":INTERFACENAME" , a.INTERFACENAME),
                                    new SugarParameter(":INTERFACEDESCRIPTION", a.INTERFACEDESCRIPTION),
                                    new SugarParameter(":INJSON", a.INJSON),
                                    new SugarParameter(":JAVASCRIPTCODE", a.JAVASCRIPTCODE),
                                    new SugarParameter(":OUTJSON", a.OUTJSON),
                                    new SugarParameter(":CLASSNAME", a.CLASSNAME),
                                    new SugarParameter(":FUNCTIONNAME", a.FUNCTIONNAME),
                                    new SugarParameter(":ID", a.ID),
                                    new SugarParameter(":ISDELETE", a.ISDELETE),
                                    new SugarParameter(":CREATEDATE", a.CREATEDATE),
                                    new SugarParameter(":CREATEUSER", a.CREATEUSER),
                                    new SugarParameter(":UPDATEDATE", a.UPDATEDATE),
                                    new SugarParameter(":UPDATEUSER", a.UPDATEUSER),
                                    new SugarParameter(":SCRIPTTYPE", a.SCRIPTTYPE),
                                    new SugarParameter(":INVALIDFLAG", a.INVALIDFLAG),
                            };
                            db.Ado.ExecuteCommand(insertInterfaceToolSql, param);
                        }
                    });

                    db.Ado.CommitTran();
                }
                catch (Exception ex)
                {
                    db.Ado.RollbackTran();
                    result.Result = false;
                    result.ErrMsg = ex.Message;
                    
                }

            }
            catch (Exception ex)
            {
                result.Result = false;
                result.ErrMsg = ex.Message;
               
            }
            return result;
        }

        #endregion
    }
}

